package utils

import (
	"fmt"
	"strings"
	"time"
)

func GetTimeString(date time.Time, short ...bool) string {
	hour, min, sec := date.Clock()
	if len(short) > 0 && short[0] {
		return fmt.Sprintf("%02d:%02d:%02d", hour, min, sec)
	} else {
		return fmt.Sprintf("%02d:%02d:%02d", hour, min, sec)
	}
}

func GetDateString(date time.Time, short ...bool) string {
	year, mon, day := date.Date()
	if len(short) > 0 && short[0] {
		return fmt.Sprintf("%04d-%02d-%02d", year, mon, day)
	} else {
		return fmt.Sprintf("%04d-%02d-%02d", year, mon, day)
	}
}

func GetDateTimeString(date time.Time, short ...bool) string {
	year, mon, day := date.Date()
	hour, min, sec := date.Clock()
	if len(short) > 0 && short[0] {
		return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", year, mon, day, hour, min, sec)
	} else {
		return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", year, mon, day, hour, min, sec)
	}
}

func TimeCost(callback func(), names ...string) {
	now := time.Now().UnixMicro()

	defer func() {
		cost := time.Now().UnixMicro() - now
		if len(names) > 0 {
			head := "execute task[" + strings.Join(names, "][") + "]"
			fmt.Println(head, "cost", cost, "us")
		} else {
			fmt.Println("cost", cost, "us")
		}
	}()

	callback()
}
